package com.example.demo;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        //1.参数校验
        //2.验证密码是否正确
        //3.进行返回
        //校验方法一
//        if(userName == null|| "".equals(userName)||password ==null|| "".equals(password)){
//            return false;
//        }
        //校验方法二
        if(StringUtil.isNullOrEmpty(userName)||StringUtil.isNullOrEmpty(password)){
            return false;
        }
        //验证密码是否正确，理论来说，应该访问数据库
        //暂且硬编码，写死
        if("admin".equals(userName)&&"admin".equals(password)){
            session.setAttribute("loginUserName", userName);
            return true;
        }
        return false;
    }
    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session){
        return (String)session.getAttribute("loginUserName");
    }
}
